package com.hbmcc.heshenghuo.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ViewFlipper
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import com.hbmcc.heshenghuo.R

/**
 * Created by Veyron on 2017/2/20.
 * Function：自定义ViewFlipper控件
 */
class UpView(context: Context, attrs: AttributeSet?) : ViewFlipper(context, attrs) {
    private fun init(context: Context?) { //设置时间间隔2000毫秒
        flipInterval = 5000 //进来的动画
        val animIn = AnimationUtils.loadAnimation(context, R.anim.anim_in)
        inAnimation = animIn //退出的动画
        val animOut = AnimationUtils.loadAnimation(context, R.anim.anim_out)
        outAnimation = animOut
    }

    /**
     * 设置循环滚动的View数组
     */
    fun setViews(views: List<View>?) {
        if (views.isNullOrEmpty()) return

        removeAllViews()
        for (item in views) {
            if (item.parent != null) (item.parent as ViewGroup).removeView(item)

            addView(item)
        }
        if (views.size > 1)  //开启翻滚
            startFlipping()
    }

    init {
        init(context)
    }
}